//
//  XHZhangShangViewModel.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-29.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHZhangShangViewModel.h"
#import "XHTableViewCell.h"
#import "XHURLString.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "customtabViewCell.h"

@interface XHZhangShangViewModel()
{
    BOOL isload;
}
@property (nonatomic ,strong) XHURLString *urlString;


@end

@implementation XHZhangShangViewModel
@synthesize defId;
- (instancetype)init:(NSString* )stf huiming:(NSString*)huiming
{
    self = [super init];
    if (self) {
        self.urlString = [[XHURLString alloc]init];
        self.hasLoad = NO;
        self.titleArray = [NSArray array];
        self.dataArray = [NSMutableArray array];
        self.tempdataArray = [NSMutableArray array];
        defId = huiming;
        huiming = nil;
        isload = NO;
        self.hasLoad = YES;
        [self pullData1:stf huiming:nil];
        
    }
    return self;
}

-(void)pullData1:(NSString*)stf huiming:(NSString*)huiming
{
    NSLog(@"pu");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* stinmg;
    if ([stf length] >0)
    {
       // NSLog(@"newdid =%@",defId);
        stinmg = stf;
    }
    else if([huiming length] > 0)
    {
         stinmg = @"4";
    }
    else
    {
        stinmg = @"3";
    }
    [manager GET:[self.urlString getColumnsList:stinmg] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            self.titleArray = (NSArray *)responseObject;
            NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
            if([stinmg isEqualToString:@"3"])
            {
                [defa removeObjectForKey:@"政务栏目"];
                [defa setObject:self.titleArray forKey:@"政务栏目"];
                
            }
            else
            {
                [defa removeObjectForKey:@"官方栏目"];
                [defa setObject:self.titleArray forKey:@"官方栏目"];

            }
            [defa synchronize];
            //NSLog(@"%@",(NSArray*)responseObject);
            for (id item in self.titleArray)
            {
                
                for (id  item1 in item)
                {
                    // NSLog(@" %@ = %@",item1,item[item1]);
                }
            }
            self.hasLoad = YES;
            if ([defId length] <= 0)
            {
                NSLog(@"deif > 0");
                [self pullData:stf huiming:huiming];
            }
            else
            {
                [self pullData3];
            }
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        self.hasLoad = YES;
        NSLog(@"2222%@",error);
    }];
    
}
-(void)canall
{
      [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
}

- (void)pullData3//县区
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
   
    
    [manager GET:[self.urlString getNewsWithColumnId:@31 wf:COUTURYCODE] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {//复制时替换成 县区code;
        if ([responseObject isKindOfClass:[NSArray class]]) {
        [self.dataArray addObject:responseObject];
             NSLog(@"县区= %@",responseObject);
            [self pullData4];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}

- (void)pullData4//地市
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:@32 wf:CITYCODE] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {//复制时替换成 地市 code
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            //NSLog(@"%@",responseObject);
            [self pullData5];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}
- (void)pullData5//山西
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:@33 wf:@"140000"] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            //NSLog(@"%@",responseObject);
            [self pullData6];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}
- (void)pullData6//全国
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:@34 wf:@"100000"] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            //NSLog(@"%@",responseObject);
            NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
            if ([defId length] > 0)
            {
                [defa removeObjectForKey:@"官方新闻"];
                [defa setObject:self.dataArray forKey:@"官方新闻"];
            }
            else
            {
                [defa removeObjectForKey:@"政务新闻"];
                [defa setObject:self.dataArray forKey:@"政务新闻"];
            }
            [defa synchronize];
            isload = YES;
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}

-(void)pullData7//
{
    
}
- (void)pullData:(NSString*)stf huiming:(NSString*)huiming;
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSString* stinmg;
    if ([stf length] >0)
    {
        stinmg = stf;
    }
    else if([huiming length] > 0)
    {
        stinmg = @"4";
    }
    else
    {
        stinmg = @"3";
    }
    
    
    [manager GET:[self.urlString getZhenwuNEW:stinmg] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            
            self.dataArray = (NSMutableArray *)responseObject;
            NSUserDefaults* fefe = [ NSUserDefaults standardUserDefaults];
            [fefe removeObjectForKey:@"政务新闻"];
            [fefe setObject:self.dataArray forKey:@"政务新闻"];
            [fefe synchronize];
            isload = YES;
            //NSLog(@"%@",responseObject);
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
    
         return [self.titleArray count];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string  = @"onepeace";
    /*XHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"XHTableViewCell" owner:self options:nil];
        cell= [array objectAtIndex:0];
    }
    if ([self.dataArray count] >0) {
        cell.title.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
        cell.describe.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"description"];
        NSString *imageString = [NSString stringWithFormat:@"%@%@",self.urlString.ipAndPort,[[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"image"]];
        
        [cell.image setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic"]];
        id hits = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"hits"];
        if ( hits) {
            cell.hits.text = [NSString stringWithFormat:@"%@",hits];
        }

    }
    return cell;*/
    customtabViewCell* cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell)
    {
        cell = [[customtabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    id tempArra ;
    if (isload)
    {
        tempArra = self.dataArray;
    }
    else
    {
        tempArra = self.tempdataArray;
        NSLog(@"缓存1 ");
    }

    if ([tempArra count] > 0 && indexPath.row < [tempArra count] )
    {
              //NSLog(@"class = %@",[[self.dataArray objectAtIndex:indexPath.row] class]);
        id tempar = [tempArra objectAtIndex:indexPath.row];
        if ([tempar count] <= 0)
        {
            return cell;
        }
        if ([defId length] >0 )
        {
            
             [cell setdic:[[tempArra objectAtIndex:indexPath.row] objectAtIndex:0]];
        }
        else
        {
              [cell setdic:[tempArra objectAtIndex:indexPath.row]];
        }
       
    }
   
    [cell setLan:[[self.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

    
}


@end
